import static com.google.appengine.api.datastore.FetchOptions.Builder.*

import org.json.simple.JSONArray
import org.json.simple.JSONObject
import org.json.simple.JSONValue

import com.google.appengine.api.datastore.*

println """
<html>
	<head>
		<title>Test</title>
	</head>
	<body>
	<pre>
"""
println '------------------------------------------------------------------------------'
println 'Testbed'
println '------------------------------------------------------------------------------'

/*
{
	cols: [{id: 'A', label: 'NEW A', type: 'string'},
		   {id: 'B', label: 'B-label', type: 'number'},
		   {id: 'C', label: 'C-label', type: 'date'}
		  ],
	rows: [{c:[{v: 'a'}, {v: 1.0, f: 'One'}, {v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}]},
		   {c:[{v: 'b'}, {v: 2.0, f: 'Two'}, {v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}]},
		   {c:[{v: 'c'}, {v: 3.0, f: 'Three'}, {v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}]}
		  ],
	p: {foo: 'hello', bar: 'world!'}
  }
{
	"cols":[
			{"id":"id","label":"Id","type":"number"},
			{"id":"amount","label":"Amount","type":"number"},
			{"id":"description","label":"Description","type":"string"},
			{"id":"createdTime","label":"Date Time","type":"string"}
		],
	"rows":[
			{"c":[{"v":136},{"v":176.0},{"v":"kulcha with paneer curry and soup at au bon pain"},{"v":"Wed Oct 26 09:48:17 UTC 2011"}]},
			{"c":[{"v":133},{"v":793.0},{"v":"the efforless life at zenhabits.net"},{"v":"Wed Oct 26 09:47:45 UTC 2011"}]}
		]
}
*/

JSONArray jsoncols = new JSONArray()
JSONObject jsoncol = new JSONObject()
jsoncol.put('id', 'id')
jsoncol.put('label', 'Id')
jsoncol.put('type', 'number')
jsoncols.add(jsoncol)
jsoncol = new JSONObject()
jsoncol.put('id', 'amount')
jsoncol.put('label', 'Amount')
jsoncol.put('type', 'number')
jsoncols.add(jsoncol)
jsoncol = new JSONObject()
jsoncol.put('id', 'description')
jsoncol.put('label', 'Description')
jsoncol.put('type', 'string')
jsoncols.add(jsoncol)
jsoncol = new JSONObject()
jsoncol.put('id', 'createdTime')
jsoncol.put('label', 'Date Time')
jsoncol.put('type', 'string')
jsoncols.add(jsoncol)

def query = new Query('TranscationE')
query.addSort('createdTime', Query.SortDirection.DESCENDING)
query.addFilter('userId', Query.FilterOperator.EQUAL, 'vteial')
PreparedQuery preparedQuery = datastore.prepare(query)
def ttentities = preparedQuery.asIterator( withLimit(2) )
JSONArray jsonrows = new JSONArray()
ttentities.each { tentity ->
	JSONArray jsonrowvals = new JSONArray();
	
	JSONObject jsonrowval = new JSONObject();
	jsonrowval.put('v', new Long(tentity.key.id));
	jsonrowvals.add(jsonrowval)
	
	jsonrowval = new JSONObject();
	jsonrowval.put('v', new Float(tentity.amount));
	jsonrowvals.add(jsonrowval)
	
	jsonrowval = new JSONObject();
	jsonrowval.put('v', tentity.description);
	jsonrowvals.add(jsonrowval)
	
	jsonrowval = new JSONObject();
	jsonrowval.put('v', tentity.createdTime.toString());
	jsonrowvals.add(jsonrowval)
	
	JSONObject jsonrow = new JSONObject()
	jsonrow.put('c', jsonrowvals)
	
	jsonrows.add(jsonrow)
}

JSONObject jsonobj = new JSONObject()
jsonobj.put('cols', jsoncols)
jsonobj.put('rows', jsonrows)
////println JSONValue.toJSONString(expenses);
JSONValue.writeJSONString(jsonobj, out);

println """
	</pre>
	</body>
</html>
"""

